<?php
class PaymentTransaction {
    
    private $today;
    
    private $controller;
    
    private $ticketId;
    
    private $cashierId;
    private $cashierName;
    
    private $patientId;
    private $patientCode;
    
    public function __construct($controller, $ticketId) {
        $this->today = strtotime(date("Y-m-d"));
        $this->controller = $controller;
        $this->setCommonData($ticketId);
    }
    
    public function deposite($paymentId, $receiptCode, $amount, $date, $note) {                
        
        $fullDate = $date . " " . date("H:i:s");                

        // 1st: Create pay_receipt type = DEPOSITE        
        $objReceipt = Celini::newOrdo('PayReceipt');        
        $receiptData =   array(
               'ticket_id'         =>  $this->ticketId,
               'patient_id'        =>  $this->patientId,
               'patient_code'      =>  $this->patientCode,
               'cashier_id'        =>  $this->cashierId,
               'cashier_name'      =>  $this->cashierName,
               'created_time'      =>  $fullDate,
               'amount'            =>  $amount,
               'note'              =>  $note,
               'enum_payment_type' =>  EnumerationConstant::PAYMENT_COLLECTION_TYPE_COLLECTING_KEY,
               'receipt_code'      =>  $receiptCode
        );
        $objReceipt->populateArray($receiptData);
        $objReceipt->persist();
        
        // 2nd: Create pay_receipt_item of DEPOSITE
        $objCreditItem  =   Celini::newORDO('PayPaymentItem')->getCreditAccountItem($paymentId);
        $receiptItemData =   array(
                'pay_receipt_id'        =>   $objReceipt->get_id(),
                'amount'                =>   $amount,
                'pay_payment_item_id'   =>   $objCreditItem->get_id(),
                'enum_item_type'        =>   $objCreditItem->get('enum_item_type'),
                'service_id'            =>   $objCreditItem->get('service_id'),
                'service_name'          =>   $objCreditItem->get('service_name'),
                'quantity'              =>   $objCreditItem->get('quantity'),
                'doctor_id'             =>   $objCreditItem->get('doctor_id')
        );
        
        // Receipt item of depositing an amount in credit account
        $objReceiptItem =   Celini::newORDO('PayReceiptItem');        
        $objReceiptItem->populateArray($creditItemData);
        $objReceiptItem->persist();
            
        // 3rd: Update realpayment in credit account (pay_payment_item)
        $objCreditItem->updateRealPayment($objCreditItem->get_id(), $amount);

        // TODO Refactor this piece of code
        // This funciton depends on the creation of pay_receipt_item
        // Update total real payment
        // 4th: Update pay_payment
        $objReceipt->updateTicketRealPayment($ticketId);
        
        // Logging receipt items if receipt's collecting date older than today
        $collectedTimeStamp = strtotime($date);
        if ($collectedTimeStamp < $this->today) {
            $receiptItemList   =   Celini::newORDO('PayReceiptItem')->getReceiptItems($objReceipt->get_id());
            $this->loggingReceiptItems($receiptItemList, EnumerationConstant::PAYMENT_LOGGING_ACTION_OLD_RECEIPT_KEY);
        }
                
    }
    
    public function delete() {
        
    }
    
    public function transfer($from, $to, $amount, $date) {
        
    }
    
    public function repaid() {
        
    }
    
    protected function setCommonData($ticketId) {
        $this->ticketId = $ticketId;
        $this->patientId   =   Celini::newORDO('Ticket', $ticketId)->getPatientId();
        $this->patientCode =   Celini::newORDO('Patient', $patientId)->getPatientCode();
        $this->cashierId   =   $this->controller->_me->get_person_id();
        $this->cashierName =   Celini::newORDO('Employee', $cashierId)->get('name');
    }
    
    ///////////////////////////////////////////////////////////////
    
    private function loggingReceiptItems($receipItemList, $action) {
        foreach ($receipItemList as &$item) {
            $item['enum_action_type']   =   $action;
            $objService =   Celini::newORDO('Service', $item['service_id']);
            $code   =   $objService->get('code');
            if (!empty($code)) {
                $item['service_code']   =   $code;
            }
            else {
                $item['service_code']   =   $item['service_name'];
            }
            $objReceipt =   Celini::newORDO('PayReceipt', $item['pay_receipt_id']);
            $item['patient_id'] =   $objReceipt->get('patient_id');
            $objPerson  =   Celini::newORDO('Person', $item['patient_id']);
            $item['patient_name']   =   $objPerson->get('name');
            $objPerson  =   Celini::newORDO('Employee', $item['doctor_id']);
            $item['doctor_name']    =   $objPerson->get('name');
            $item['date_collect']   =   $objReceipt->get('created_time');
            $item['date_modified']  =   date('Y-m-d H:i:s');
            $item['user_modified']  =   $this->controller->_me->get_username();

            $objLogging =   Celini::newORDO('PayReceiptLog');
            $objLogging->populateArray($item);
            $objLogging->persist();
        }
    }
    
}